package com.ucode.springboot.starter.security.entity;

import java.util.Collection;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * SpringSecurity需要的用户详情
 * @author: liliang
 * @date: 2019年12月19日 下午8:50:54
 */
public class UcodeUserDetails implements UserDetails{
    private static final long serialVersionUID = 712478115779676121L;
    
    private UcodeUser user;
    
    public UcodeUserDetails(UcodeUser user){
        this.user = user;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return user.getAuthoritys();
    }

    @Override
    public String getPassword() {
        return this.user != null ? this.user.getPassword() : null;
    }

    @Override
    public String getUsername() {
        return this.user != null ? this.user.getUsername() : null;
    }

    /**
     * 账户是否有效
     */
    @Override
    public boolean isAccountNonExpired() {
        return this.user != null ? this.user.isAccountNonExpired() : false;
    }

    /**
     * 账户是否被锁定
     */
    @Override
    public boolean isAccountNonLocked() {
        return this.user != null ?  this.user.isAccountNonLocked() :false;
    }

    /**
     * 密码是否有效
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return this.user != null ?  this.user.isCredentialsNonExpired() :false;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    public UcodeUser getUser() {
        return user;
    }

    public void setUser(UcodeUser user) {
        this.user = user;
    }
}
